package com.hay.oss.ali;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.hay.oss.OssConstant;
import com.hay.oss.OssTemplate;
import com.hay.oss.bo.OssFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.io.InputStream;

/**
 * 文档地址 https://help.aliyun.com/zh/oss
 * @author LiMouRen
 * @date 2023/7/26
 */
@Slf4j
public class AliOssTemplate implements OssTemplate {

    private final AliOssProperties ossProperties;

    private final OSSClient ossClient;

    private final String bucketName;

    public AliOssTemplate(AliOssProperties ossProperties, OSSClient ossClient){
        this.ossProperties = ossProperties;
        this.ossClient = ossClient;
        bucketName = ossProperties.getBucketName();
    }

    public OSSClient getClient(){
        return this.ossClient;
    }

    /**
     * 上传文件
     * @param category 文件前缀,如 product/2024/01/15
     * @param originalFileName 文件原名
     * @param input 流
     * @param useOriginalName true 使用原名
     */
    @Override
    public OssFile uploadFile(String category, String originalFileName, InputStream input, Boolean useOriginalName){
        String relativeUrl = createRelativeUrl(category,originalFileName,useOriginalName);
        ossClient.putObject(bucketName, relativeUrl, input);
        String link = this.getOssHost() + OssConstant.SLASH + relativeUrl;
        OssFile ossFile = new OssFile();
        ossFile.setLink(link);
        ossFile.setOriginalName(originalFileName);
        ossFile.setRelativeUrl(relativeUrl);
        return ossFile;
    }

    /**
     * 获取输入流
     * @param relativeUrl 相对地址，如 product/20240115/1.jpg
     * @return
     */
    @Override
    public InputStream getInputStream(String relativeUrl) {
        OSSObject fileOss = getFileOss(bucketName, relativeUrl);
        Assert.notNull(fileOss,"文件不存在");
        return fileOss.getObjectContent();
    }

    /**
     * 删除文件
     * @param relativeUrl 相对地址，如 product/20240115/1.jpg
     */
    @Override
    public void removeFile(String relativeUrl){
        ossClient.deleteObject(bucketName, relativeUrl);
    }

    /**
     * 根据文件相对路径获取文件流
     * @param relativeUrl 相对地址
     */
    public OSSObject getFileOss(String bucketName,String relativeUrl){
        return ossClient.getObject(bucketName, relativeUrl);
    }


    /**
     * 获取主机
     * @return
     */
    public String getOssHost(){
        String host = null;
        String hostMode = ossProperties.getHostMode();
        if (OssConstant.HOST_BUCKET.equals(hostMode)){
            String prefix = this.ossProperties.getEndpoint().contains(OssConstant.HTTPS_PREFIX) ? OssConstant.HTTPS_PREFIX : OssConstant.HTTP_PREFIX;
            host =  prefix + bucketName + OssConstant.DOT + this.ossProperties.getEndpoint().replaceFirst(prefix, "");
        } else if(OssConstant.HOST_PROXY.equals(hostMode)){
            host =  ossProperties.getProxyPrefix();
        } else {
            host =  ossProperties.getEndpoint();
        }
        return host;
    }

    /**
     * 压缩单张图片图片
     * relativeUrl 与 new relativeUrl 相等时会覆盖原文件
     */
    public OSSObject compressSinglePic(String relativeUrl, String newRelativeUrl, Integer quality) {
        String style = "image/format,jpg/quality,q_"+ quality;
        GetObjectRequest request = new GetObjectRequest(bucketName, relativeUrl);
        request.setProcess(style);
        OSSObject compressObject = ossClient.getObject(request);
        ossClient.putObject(bucketName, newRelativeUrl,compressObject.getObjectContent());
        return compressObject;
    }


}


